from tkinter import Tk
from database.table import tablee
from database.table.tablee import Purchases
import tkinter as tk

def DeletePurchase():
    delete_purchase_screen = Tk()
    delete_purchase_screen.title('删除员工信息')
    screen_width, screen_height = delete_purchase_screen.maxsize()

    # 窗口的宽和高
    width = 605
    height = 200
    centre = '%dx%d+%d+%d' % (width, height, (screen_width - width) / 2,
                              (screen_height - height) / 2)
    delete_purchase_screen.geometry(centre)
    delete_purchase_screen.resizable(0, 0)

    label = tk.Label(delete_purchase_screen, text='请输入要删除的采购信息编号：', font=('楷体', 14))
    label.place(x=150, y=20)

    # 编号标签文本框
    user_pno_label = tk.Label(delete_purchase_screen, text='编号', font=('楷体', 20))
    user_pno_label.place(x=150, y=60)
    user_pno_text = tk.StringVar()
    user_pno_entry = tk.Entry(delete_purchase_screen, textvariable=user_pno_text,
                              width=15)
    user_pno_entry.place(x=230, y=68)

    # 确定按钮响应事件
    def confirm():
        id = str(user_pno_entry.get())
        purchasee = tablee.session.query(Purchases).filter(Purchases.Pno == id).all()
        data = Purchases.to_json(purchasee)
        length = len(data)

        if length == 1:
            purchasee = tablee.session.query(Purchases).get(int(id))
            tablee.session.delete(purchasee)
            tablee.session.commit()
            tablee.session.close()

            delete = tk.Toplevel(delete_purchase_screen)
            delete.title("Success")
            screen_width, screen_height = delete.maxsize()
            # 窗口的宽和高
            width = 240
            height = 100
            centre = '%dx%d+%d+%d' % (width, height, (screen_width - width) / 2,
                                      (screen_height - height) / 2)
            delete.geometry(centre)
            label = tk.Label(delete, text='删除成功！', font=('楷体', 14))
            label.place(x=60, y=15)

            def confirmm():
                delete.destroy()

            # 确定按钮
            user_confirm_button = tk.Button(delete, text='确定', font=('楷体', 12),
                                            width=10, command=confirmm)
            user_confirm_button.place(x=65, y=60)

        elif id == '':
            delete = tk.Toplevel(delete_purchase_screen)
            delete.title("Error")
            screen_width, screen_height = delete.maxsize()
            # 窗口的宽和高
            width = 400
            height = 100
            centre = '%dx%d+%d+%d' % (width, height, (screen_width - width) / 2,
                                      (screen_height - height) / 2)
            delete.geometry(centre)
            label = tk.Label(delete, text='编号不能为空，请重新输入！', font=('楷体', 14))
            label.place(x=60, y=15)

            def confirmm():
                delete.destroy()

            # 确定按钮
            user_confirm_button = tk.Button(delete, text='确定', font=('楷体', 12),
                                            width=10, command=confirmm)
            user_confirm_button.place(x=140, y=60)

        else:
            delete = tk.Toplevel(delete_purchase_screen)
            delete.title("Error")
            screen_width, screen_height = delete.maxsize()
            # 窗口的宽和高
            width = 400
            height = 100
            centre = '%dx%d+%d+%d' % (width, height, (screen_width - width) / 2,
                                      (screen_height - height) / 2)
            delete.geometry(centre)
            label = tk.Label(delete, text='此编号不存在,请重新输入！', font=('楷体', 14))
            label.place(x=60, y=15)

            def confirmm():
                delete.destroy()

            # 确定按钮
            user_confirm_button = tk.Button(delete, text='确定', font=('楷体', 12),
                                            width=10, command=confirmm)
            user_confirm_button.place(x=140, y=60)

    # 返回按钮响应事件
    def back():
        delete_purchase_screen.destroy()

    # 确定按钮
    user_confirm_button = tk.Button(delete_purchase_screen, text='确定', font=('楷体', 12),
                                    width=10, command=confirm)
    user_confirm_button.place(x=150, y=120)
    # 返回按钮
    user_back_button = tk.Button(delete_purchase_screen, text='返回', font=('楷体', 12),
                                 width=10, command=back)
    user_back_button.place(x=280, y=120)

    delete_purchase_screen.mainloop()


if __name__ == '__main__':
    DeletePurchase()
